package com.ailanyin.admin.service.impl;

import com.ailanyin.admin.service.SysPostService;
import com.ailanyin.admin.service.SysUserService;
import com.ailanyin.common.exception.Asserts;
import com.ailanyin.common.utils.EmptyUtil;
import com.ailanyin.mapper.SysPostMapper;
import com.ailanyin.mapper.SysUserPostMapper;
import com.ailanyin.model.SysPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/15 0015 下午 14:43
 */
@Service
public class SysPostServiceImpl implements SysPostService {

    @Autowired
    private SysUserService sysUserService;

    @Resource
    private SysPostMapper postMapper;
    @Resource
    private SysUserPostMapper userPostMapper;

    /**
     * 增加岗位
     *
     * @param post post
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertPost(SysPost post) {
        if (checkPostNameNotUnique(post)) {
            Asserts.fail("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (checkPostCodeNotUnique(post)) {
            Asserts.fail("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(sysUserService.getCurrentUser().getUserName());
        post.setCreateTime(new Date());
        postMapper.insertPost(post);
    }

    /**
     * 修改岗位
     *
     * @param post post
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePost(SysPost post) {
        if (checkPostNameNotUnique(post)) {
            Asserts.fail("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (checkPostCodeNotUnique(post)) {
            Asserts.fail("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(sysUserService.getCurrentUser().getUserName());
        post.setUpdateTime(new Date());
        postMapper.updatePost(post);
    }

    /**
     * 批量删除岗位
     *
     * @param postIds ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePostByIds(Long[] postIds) {
        for (Long postId : postIds) {
            SysPost post = selectPostById(postId);
            if (countUserPostById(postId) > 0) {
                Asserts.fail(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        postMapper.deletePostByIds(postIds);
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param postId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public SysPost selectPostById(Long postId) {
        return postMapper.selectPostById(postId);
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserPostById(Long postId) {
        return userPostMapper.countUserPostById(postId);
    }

    /**
     * 查询全部岗位
     *
     * @return 全部岗位集合
     */
    @Override
    public List<SysPost> selectPostAll() {
        return postMapper.selectPostAll();
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Integer> selectPostListByUserId(Long userId) {
        return postMapper.selectPostListByUserId(userId);
    }

    /**
     * 查询岗位列表
     *
     * @param post 查询条件
     * @return List<SysPost>
     */
    @Override
    public List<SysPost> selectPostList(SysPost post) {
        return postMapper.selectPostList(post);
    }

    /**
     * 校验岗位名称
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostNameNotUnique(SysPost post) {
        SysPost info = postMapper.checkPostNameUnique(post.getPostName());
        return EmptyUtil.isNotEmpty(info) && !info.getPostId().equals(post.getPostId());
    }

    /**
     * 校验岗位编码
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostCodeNotUnique(SysPost post) {
        SysPost info = postMapper.checkPostCodeUnique(post.getPostCode());
        return EmptyUtil.isNotEmpty(info) && !info.getPostId().equals(post.getPostId());
    }
}
